﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emergency4.Dal;
using Emergency4.From;
using Emergency4.Session;

namespace Emergency4.From
{        /// <summary>
         /// 员工 Asset窗口
         /// </summary>
    public partial class EmergencyMaintenanceManagement : Form
    {
        public EmergencyMaintenanceManagement()
        {
            InitializeComponent();
        }

        private void EmergencyMaintenanceManagement_Load(object sender, EventArgs e)
        {   //查询所有资产信息
            List<Hashtable> list = AssetDal.findAllAsset();
            showAllInfo(list);
            //当点击内容时 调用事件处理的适配器
            dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.SelectedCells.Count==1 && e.RowIndex >= 0)
            {
                //如果需要引用必要的值进行比较需要将 左边转化为string类型
                if(dataGridView1.SelectedCells[0].Value == "Detail")
                {
                    EmergencyMaintenanceRequest rq = new EmergencyMaintenanceRequest();
                    rq.id = int.Parse(dataGridView1.SelectedCells[0].OwningRow.Tag.ToString());
                    rq.Show();
                    //当关闭该窗体时，触发关闭事件，并调用相应的事件
                    rq.FormClosed += new FormClosedEventHandler(this_form);
                }
            }
        }

        private void this_form(object sender, FormClosedEventArgs e)
        {
            List<Hashtable> list = AssetDal.findAllAsset();
            showAllInfo(list);
        }

        string[] header = new string[] { "AssetSN", "AssetName", "Last Closed EM", "Number Of EMS" };
        /// <summary>
        /// 展示用户信息
        /// </summary>
        /// <param name="listAssets"></param>
        private void showAllInfo(List<Hashtable> list)
        {
           dataGridView1.Rows.Clear();//清除表格中的行 数据
           dataGridView1.Columns.Clear();//清除表格中的列 数据
            //自动适应  表格中 行 和 列
           dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
           dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

           dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
           dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; //表头设置

            //遍历字符数组，
            foreach(string item in header)
            {
                //实例化一个表头（列）
                DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
                c.FillWeight = 500;//设置高
                c.Name = item;//每列的名称
                //添加到表格中
                dataGridView1.Columns.Add(c);
            }
            //表格中列的 链接 （请求）
            DataGridViewLinkColumn c2 = new DataGridViewLinkColumn();
            c2.FillWeight = 500;
            c2.Name = "Operation";
            dataGridView1.Columns.Add(c2);

            for(int i = 0; i < list.Count; i++)
            {
                //向每行中添加相应的数据
                dataGridView1.Rows.Add(list[i]["assetSN"],list[i]["assetName"],list[i]["date"],list[i]["countEM"],"Detail");
                dataGridView1.Rows[i].Tag = list[i]["id"];//每行存一个id
                //判断此处的状态，false表示 有
                if (list[i]["flag"].Equals(false))
                {   //符合条件就设置该行为红色
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
            }

        }

    }
}
